  
<cfcomponent displayname="usersDAO" hint="I abstract data access for users.">

	<cffunction name="init" access="public" returntype="usersDAO" output="false" displayname="users Constructor" hint="I initialize a users.">
		<cfargument name="datasourceSettings" type="any" required="true" />
		
		<cfset variables.dsn = arguments.datasourceSettings.getConfigSetting("dsn") />
		<cfset variables.dbtype = arguments.datasourceSettings.getConfigSetting("dbtype") />

		<cfreturn this />
	</cffunction>

 
	<cffunction name="save" returntype="any" output="false" access="public" 	hint="I save a users">
		<cfargument name="users" type="users" hint="I am the users to be saved" required="true"/>

		<cfif arguments.users.hasId()>
			<cfif arguments.users.getuserID() neq ''>
				<cfreturn update(arguments.users)/>
			<cfelse>
				<cfreturn create(arguments.users)/>
			</cfif>
		<cfelse>
			<cfreturn create(arguments.users)/>
		</cfif>

	</cffunction>

	<cffunction name="create" access="remote" returntype="any" output="false">
		<cfargument name="usersBean" type="users" required="true" hint="I am the users object into which data is created." />/>
		<cfset usersBean.setuserID(createUUID())>

		<cfquery name="insRec" datasource="#variables.dsn#">
			insert into users(
			userID, email, password, passwordSalt, firstName, lastName, userName, dtLastAccess, ipLastAccess, dtCreated, ipCreated, createdByID, dtModified, ipModified, modifiedByID, isActive, isSysAdmin, isAdmin
			)
			values(
				<cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getUserID()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getemail()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getpassword()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getpasswordSalt()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getfirstName()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getlastName()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getuserName()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#dateformat(arguments.usersBean.getdtLastAccess(),'short')# #timeformat(arguments.usersBean.getdtLastAccess(),'short')#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getipLastAccess()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#dateformat(arguments.usersBean.getdtCreated(),'short')# #timeformat(arguments.usersBean.getdtCreated(),'short')#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getipCreated()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getcreatedByID()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#dateformat(arguments.usersBean.getdtModified(),'short')# #timeformat(arguments.usersBean.getdtModified(),'short')#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getipModified()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getmodifiedByID()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.usersBean.getisActive()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.usersBean.getisSysAdmin()#"  />
				, <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.usersBean.getisAdmin()#"  />
				
				)
		</cfquery>
		
		
		
		
		<cfreturn usersBean/>
	</cffunction>
	
	<cffunction name="read" access="public" returntype="any" output="false" hint="CRUD method read.">
		<cfargument name="userID" type="string" required="true" hint="I am the ID of the database record to read." />
		<cfset var usersBean = createobject("component","users").init() />
		<cfset var usersSelect =false />
		<cfquery name="usersSelect" datasource="#variables.dsn#">
			SELECT * 
			FROM	users	
			WHERE
				userID = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.userID#"  /> 
				
		</cfquery>

		<cfif usersSelect.recordCount >
			<cfscript>
				usersBean.setuserID(usersSelect.userID);				
				usersBean.setemail(usersSelect.email);				
				usersBean.setpassword(usersSelect.password);				
				usersBean.setpasswordSalt(usersSelect.passwordSalt);				
				usersBean.setfirstName(usersSelect.firstName);				
				usersBean.setlastName(usersSelect.lastName);				
				usersBean.setdtLastAccess(usersSelect.dtLastAccess);				
				usersBean.setipLastAccess(usersSelect.ipLastAccess);				
				usersBean.setdtCreated(usersSelect.dtCreated);				
				usersBean.setipCreated(usersSelect.ipCreated);				
				usersBean.setcreatedByID(usersSelect.createdByID);				
				usersBean.setdtModified(usersSelect.dtModified);				
				usersBean.setipModified(usersSelect.ipModified);				
				usersBean.setmodifiedByID(usersSelect.modifiedByID);				
				usersBean.setisActive(usersSelect.isActive);				
				usersBean.setisSysAdmin(usersSelect.isSysAdmin);
				usersBean.setisAdmin(usersSelect.isAdmin);
				usersBean.setusername(usersSelect.username);
			</cfscript>			
			
		</cfif>
		
		<cfreturn usersBean />
	</cffunction>

	<cffunction name="update" returntype="any" output="false" hint="CRUD method update">
		<cfargument name="usersBean" type="users" required="true" />	
		<cfset var usersUpdate = 0 >

		<cfquery name="usersUpdate" datasource="#variables.dsn#" >
			UPDATE users
			SET
				email = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getemail()#"  /> 
				, password = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getpassword()#"  /> 
				, passwordSalt = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getpasswordSalt()#"  /> 
				, firstName = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getfirstName()#"  /> 
				, lastName = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getlastName()#"  /> 
				, dtLastAccess = <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#dateformat(arguments.usersBean.getdtLastAccess(),'short')# #timeformat(arguments.usersBean.getdtLastAccess(),'short')#"  /> 
				, ipLastAccess = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getipLastAccess()#"  /> 
				, dtCreated = <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#dateformat(arguments.usersBean.getdtCreated(),'short')# #timeformat(arguments.usersBean.getdtCreated(),'short')#"  /> 
				, ipCreated = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getipCreated()#"  /> 
				, createdByID = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getcreatedByID()#"  /> 
				, dtModified = <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#dateformat(arguments.usersBean.getdtModified(),'short')# #timeformat(arguments.usersBean.getdtModified(),'short')#"  /> 
				, ipModified = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getipModified()#"  /> 
				, modifiedByID = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getmodifiedByID()#"  /> 
				, isActive = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.usersBean.getisActive()#"  /> 
				, isSysAdmin = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.usersBean.getisSysAdmin()#"  /> 
				, isAdmin = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.usersBean.getisAdmin()#"  /> 
				, username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.usersBean.getUserName()#"  /> 
				
			WHERE
				userID = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getuserID()#"  /> 
				
		</cfquery>
		
		<cfreturn usersBean />
	</cffunction>


	<cffunction name="delete" returntype="void" output="false" hint="CRUD method delete" >
		<cfargument name="usersBean" type="users" required="true" />		

		<cfset var usersDelete = 0 >

		<cfquery name="usersDelete" datasource="#variables.dsn#" >
			DELETE FROM users
			WHERE
				userID = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.usersBean.getuserID()#"  /> 
				
		</cfquery>
	</cffunction>

</cfcomponent>

  
